/**
 * Jaxson ajax
 *
 * Copyright Remedy Interactive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */
package org.jaxson.util.servlet;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Single point of entry to programmatically subscribe to container context events.
 * 
 * @author Joe Maisel
 */
public class ServletContextEventDispatcher implements ServletContextListener
{
	private static final List<ServletContextListener> listeners = new ArrayList<ServletContextListener>();

	public static void subscribe( ServletContextListener listener )
	{
		listeners.add( listener );
	}
	
	public static void unsubscribe( ServletContextListener listener )
	{
		listeners.remove( listener );
	}

	/**
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextDestroyed( ServletContextEvent evt )
	{
		for( ServletContextListener listener : listeners )
		{
			listener.contextDestroyed( evt );
		}
	}

	/**
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextInitialized( ServletContextEvent evt )
	{
		for( ServletContextListener listener : listeners )
		{
			listener.contextInitialized( evt );
		}
	}

}
